package com.sl.u_push_plugin

import android.app.Notification
import android.content.Context
import android.content.Intent
import android.util.Log
import com.umeng.message.PushAgent
import com.umeng.message.UmengMessageHandler
import com.umeng.message.entity.UMessage
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.MethodChannel
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable

class MessageManager private constructor() {
    var events: EventChannel.EventSink? = null
    var messageMap: MutableMap<String, Any>? = null
    var cacheMap: MutableMap<String, Any>? = null

    companion object {
        val instance by lazy {
            MessageManager()
        }
    }

    fun initEvents(events: EventChannel.EventSink?) {
        this.events = events
    }

    fun addColdStarData(key: String, value: Any): MessageManager {
        if (cacheMap == null) cacheMap = mutableMapOf()
        cacheMap?.put(key, value)
        return this
    }

    fun buildMessageMap() {
        if (messageMap == null) messageMap = mutableMapOf()
        messageMap?.put("data", cacheMap ?: mapOf<String, Any>())
    }

    fun setIntentJson(intent: Intent) {

        if (events == null) {
            var result = ResaultData().Builder()
            intent.extras?.run {
                val keySet: Set<String> = keySet()
                keySet.forEach {
                    if(getString(it)!=null){
                        result.initData(it, getString(it))
                    }
                }
                result.initType(TypeData.message)
                messageMap = result.build()
            }
        } else {
            var result = ResaultData().Builder()
            intent.extras?.run {
                val keySet: Set<String> = keySet()
                keySet.forEach {
                    if(getString(it)!=null){
                        result.initData(it, getString(it))
                    }
                }
                result.initType(TypeData.message)
                if(!result.isEmpty()){
                    events?.success(result.build())
                }
            }
        }
    }

    fun getCacheMessage(result: MethodChannel.Result?) {
        result?.success(messageMap ?: mapOf<String, String>())
        cacheMap?.clear()
        messageMap?.clear()
    }

    fun receiveMessageListener(mConetext:Context) {
        PushAgent.getInstance(mConetext).messageHandler = object: UmengMessageHandler(){
            override fun getNotification(p0: Context?, p1: UMessage?): Notification? {
                p1?.run {
                    var msgData = ResaultData().Builder();
                    extra.forEach {
                        msgData.initData(it.key,it.value)
                    }
                    msgData.initType(TypeData.receiver)
                    Observable.just(msgData).observeOn(AndroidSchedulers.mainThread()).subscribe {
                        events?.success(it.build())
                    }
                }
                return super.getNotification(p0, p1)
            }
        }
    }

    fun sendJsonMessage2Flutter(json: String) {
        events?.success(json)
    }

}